﻿using System;
using System.Collections.Generic;

namespace DarcRogue
{
    public enum ButtonFunction
    {
        NewGame,
        LoadGame,
        Help,
        Credits,
        Exit,
        Options
    }
    public class MainMenu
    {
        List<string> items = new List<string>();
        List<ButtonFunction> itemfunc = new List<ButtonFunction>();
        int selected = 0;

        public void Update()
        {
            ConsoleKeyInfo? key;
            while (items.Count > 0)
            {
                key = ReadKey();
                if (!key.HasValue)
                    break;
                switch (key.Value.Key)
                {
                    case ConsoleKey.UpArrow:
                        {
                            selected--;
                            if (selected == -1)
                                selected = items.Count - 1;
                        }
                        break;
                    case ConsoleKey.DownArrow:
                        {
                            selected++;
                            if (selected >= items.Count)
                                selected = 0;
                        }
                        break;
                    case ConsoleKey.Enter:
                    case ConsoleKey.Spacebar:
                        {
                            switch (itemfunc[selected])
                            {
                                case ButtonFunction.Credits:
                                    Program.Credits();
                                    break;
                                case ButtonFunction.Exit:
                                    Program.Exit();
                                    break;
                                case ButtonFunction.Help:
                                    Program.Help();
                                    break;
                                case ButtonFunction.LoadGame:
                                    Program.LoadGame();
                                    break;
                                case ButtonFunction.NewGame:
                                    Program.NewGame();
                                    break;
                                case ButtonFunction.Options:
                                    Program.Options();
                                    break;
                            }
                        }
                        break;
                }
            }
        }
        public void Draw()
        {
            Console.Clear();
            for (int num = 18 - (items.Count * 2); num != 0; num--)
                Console.WriteLine();
            foreach (string s in items)
            {
                if (s == items[selected])
                    Console.ForegroundColor = ConsoleColor.White;
                else
                    Console.ForegroundColor = ConsoleColor.Gray;
                int i = Console.BufferWidth - s.Length;
                i /= 2;
                Console.CursorLeft = i;
                Console.WriteLine(s);
                Console.WriteLine();
            }
        }

        public void AddItem(string s)
        {
            items.Add(s);
        }
        public void AddItemFunction(ButtonFunction s)
        {
            itemfunc.Add(s);
        }
        public void AddNewItem(string s, ButtonFunction d)
        {
            AddItem(s);
            AddItemFunction(d);
        }

        ConsoleKeyInfo? ReadKey()
        {
            if (!Console.KeyAvailable)
                return null;
            return Console.ReadKey();
        }
    }
}
